package com.service.impl;

import com.dao.RoomMapper;
import com.entity.Room;
import com.google.gson.Gson;
import com.service.RoomService;
import com.vo.RoomVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoomServiceImpl implements RoomService {

    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<RoomVo> roomAll(int page, int limit) {
        Map<String,Integer> map = new HashMap<>();
        map.put("page",page);
        map.put("limit",limit);
        List<Room> rooms = roomMapper.selectPbyL(map);
        List<RoomVo> roomVos = new ArrayList<>();
        for (Room room : rooms) {
            RoomVo roomVo = new RoomVo();
            roomVo.setRoomId(room.getRoomId());
            roomVo.setRoomName(room.getRoomName());

            roomVo.setSituation("<button class='layui-btn' id='"+room.getRoomId()+"'>显示座位</button>");

            roomVo.setSetUp("<a href='' target='content' class='layui-btn layui-btn-sm layui-btn-danger'><i class='layui-icon-set-sm'>&#xe620;</i>修改</a>");
            roomVos.add(roomVo);
        }

        return roomVos;
    }

    @Override
    public List<RoomVo> roomAll() {
        return null;
    }
    public int count(){
        return  roomMapper.selectAll().size();
    }
    @Override
    public String situation(String roomId) {
        Gson gson =new Gson();
        int[][] arr = new int[10][10];
        Room room = roomMapper.selectById(roomId);
        arr = gson.fromJson(room.getSituation(),arr.getClass());
        StringBuffer buffer = new StringBuffer();
        buffer.append("<table style='background-color: #000;margin: auto;'>");
        for (int[] a : arr) {
            buffer.append("<tr style='height: 10px;'></tr>");
            buffer.append("<tr>");

            for (int i : a) {
                buffer.append("<td style='width: 10px;'></td>");
                buffer.append("<td>");
                buffer.append("<img src=\"/imgs/icon-1.png\" style=\"width:26px;height:25px;\"/>");
                buffer.append("</td>");
            }
            buffer.append("<td style='width: 10px;'></td>");
            buffer.append("</tr>");

        }
        buffer.append("<tr style='height: 10px;'></tr>");
        buffer.append("</table>");

        return buffer.toString();
    }

    @Override
    public int insertRoom(String roomid, String name, int row, int column) {
        int[][] seat = new int[row][column];
        String jsonStr = new Gson().toJson(seat);
        Room room = new Room();
        room.setRoomId(roomid);
        room.setRoomName(name);
        room.setSituation(jsonStr);

        return roomMapper.insert(room);
    }


}
